/**
 * Created by Administrator on 2017/4/14.
 */
"use strict";
swan_pc
    .controller("OrgOppProductController",function($scope,$rootScope,$state,ngDialog,oppProduct,productNew,oppOwner,oppProductUpdate) {

        if ($rootScope.presentAccountInfo.id!=$rootScope.opp.ownerId||!$scope.$parent.isMyOpp){
            $scope.canEdit=false;
        }else {
            $scope.canEdit=true;
        }

        $scope.getList=function () {
            oppProduct.query({id:$rootScope.opp.id},function (result) {
                $scope.oppProductList=result;
                console.log("PRODUCT List:",$scope.oppProductList)
                $scope.getAllProduct();
            })
        };

        $scope.getList();

        $scope.getAllProduct=function () {
            productNew.query({page:0,size:5000,sort:'asc'},function (result) {
                $scope.productList=result;
                console.log("PRODUCT",result);
            })

        }


        $scope.initProduct=function () {
            $scope.selectProduct={
                amount:0,
                configure:'',
                id:null,
                oppId:$rootScope.opp.id,
                productNew:null,
                oppProductNewContactDTOList:[]
            }
            $scope.editProduct($scope.selectProduct,true);
        }

        $scope.deleteProduct=function (item) {
            swal("没有接口删"+item.productNew.name+"呢","","error")
        }



        $scope.editProduct=function (product,_new) {
            $scope.selectProduct=product;
            ngDialog.open({
                templateUrl:"scripts/components/organizationBusiness/myOpp/selectProduct.html",
                scope:$scope,
                controller:['$scope',function ($scope) {

                    oppOwner.query({page:0,size:5000,sort:'asc',id:$rootScope.opp.id},function (result) {
                        $scope.EOList = result;
                        // _.forEach($scope.EOList,function (item) {
                        //     _.forEach($scope.selectProduct.oppProductNewContactDTOList,function (x) {
                        //         if(x.orgContactId==item.orgContactId){
                        //             item.$$checked=true;
                        //         }
                        //     })
                        // })
                    });

                    $scope.addOwner=function () {
                        var newOwner={
                            id:null,
                            orgContactId:null,
                            orgContactName:'',
                            rate:0,
                        }
                        $scope.selectProduct.oppProductNewContactDTOList.push(newOwner);
                    };

                    $scope.deleteContact=function (index) {
                        $scope.selectProduct.oppProductNewContactDTOList.splice(index,1);
                    };

                    $scope.save=function () {
                        console.log("SAVE:",$scope.selectProduct)
                        if ($scope.selectProduct.productNew==null){
                            swal("请选择产品","","error");
                            return ;
                        }
                        if(_new==true){
                            oppProduct.save($scope.selectProduct,function (result) {
                                swal("添加成功","","success");
                                console.log("saved",result);
                                $scope.getList();
                                ngDialog.closeAll();
                            })
                        }else {
                            oppProductUpdate.update({},$scope.selectProduct,function (result) {
                                swal("更新成功","","success");
                                console.log("saved",result);
                                $scope.getList();
                                ngDialog.closeAll();
                            })
                        }
                    }

                    $scope.cancel=function () {
                        ngDialog.closeAll();
                    }
                }]
            })
        }
    })